package com.rzico.sbl.ui.report

import android.widget.ImageView
import android.widget.TextView
import androidx.core.view.isVisible
import com.rzico.sbl.R
import com.rzico.sbl.databinding.ActivityCommonListBinding
import com.rzico.sbl.model.ReportCouponData
import com.rzico.sbl.other.LinearNormalDecoration
import com.rzico.sbl.ui.report.other.goToOrderDetail
import com.rzico.sbl.viewmodel.ReportViewModel
import com.xinnuo.common.extend.*
import com.xinnuo.common.helper.*
import com.xinnuo.common_ui.base.BaseActivity
import com.xinnuo.common_ui.base.addItems
import com.xinnuo.common_ui.base.loadLinear
import com.xinnuo.common_ui.utils.bindLifeCycle
import com.xinnuo.common_ui.utils.subscribeByFinally
import com.xinnuo.slimadapter.SlimAdapter

class ReportCouponRecordActivity : BaseActivity(R.layout.activity_common_list) {

    private val mBinding by lazy { ActivityCommonListBinding.bind(getRootView()) }

    private var mCouponRecordId = ""

    override fun getViewModel() = get<ReportViewModel>()

    override fun initData() {
        initTitle(title = "使用明细")

        mCouponRecordId = intent.getExtra("index")

        initLayout()

        mBinding
            .listLayout
            .swipeRefresh
            .isRefreshing = true

        getData(pageNum)
    }

    private fun initLayout() {
        mBinding.listLayout.apply {
            emptyLayout.emptyHint.text = "暂无相关记录信息！"

            recycleList.also { layout ->
                layout.loadLinear(
                    refresh = swipeRefresh,
                    onRefresh = { getData(0) },
                    onLoad = {
                        isLoadingMore
                            .conditionFalse {
                                isLoadingMore = true
                                getData(pageNum)
                            }
                    }
                )

                layout.addItemDecoration(
                    object : LinearNormalDecoration() {
                        override fun getHeaderName(pos: Int): String =
                            "${(mList[pos] as ReportCouponData).letter}月"
                    }.apply {
                        setHeaderContentColor(getColorEx(R.color.background))
                        setTextColor(getColorEx(R.color.black))

                        setHeaderHeight(dp2px(40f))
                        setFooterHeight(dp2px(13f))
                        setTextSize(sp2px(15f))
                    }
                )

                mAdapter = SlimAdapter.creator()
                    .register<ReportCouponData>(R.layout.item_report_coupon_record) { jector, bean, _ ->
                        jector.apply {
                            text(R.id.item_coupon_name, bean.content)
                            text(R.id.item_coupon_time, bean.create_date.getTimeString())
                            text(
                                R.id.item_coupon_last,
                                when (intent.getExtra("index")) {
                                    "4" -> "上期余额：${bean.stock.formatDecimal()}"
                                    "3" -> "上期剩余：${bean.stock.formatDecimalWhenHave()}"
                                    else -> "上期库存：${bean.stock.formatDecimalWhenHave()}"
                                }
                            )
                            text(
                                R.id.item_coupon_leave,
                                when (intent.getExtra("index")) {
                                    "4" -> "本期余额：${bean.balance.formatDecimal()}"
                                    "3" -> "本期剩余：${bean.balance.formatDecimalWhenHave()}"
                                    else -> "本期库存：${bean.balance.formatDecimalWhenHave()}"
                                }
                            )

                            with<ImageView>(R.id.item_coupon_img) { ivImg ->
                                ivImg.setImageResource(
                                    when (bean.type) {
                                        "0" -> R.mipmap.icon_report13
                                        "2" -> R.mipmap.icon_report10
                                        "3" -> R.mipmap.icon_report11
                                        else -> R.mipmap.icon_report12
                                    }
                                )
                            }

                            with<TextView>(R.id.item_coupon_num) {
                                val amount = bean.balance.doubleValue - bean.stock.doubleValue
                                it.setTextColor(
                                    getColorEx(
                                        conditionIf(
                                            amount < 0,
                                            R.color.red,
                                            R.color.gray
                                        )
                                    )
                                )

                                when (mCouponRecordId) {
                                    "4" ->
                                        it.text = conditionIf(
                                            amount < 0,
                                            "${amount.formatDecimal()} 元",
                                            "+${amount.formatDecimal()} 元"
                                        )
                                    "3" ->
                                        it.text = conditionIf(
                                            amount < 0,
                                            "${amount.formatDecimalWhenHave()} 只",
                                            "+${amount.formatDecimalWhenHave()} 只"
                                        )
                                    else ->
                                        it.text = conditionIf(
                                            amount < 0,
                                            "${amount.formatDecimalWhenHave()} 张",
                                            "+${amount.formatDecimalWhenHave()} 张"
                                        )
                                }
                            }

                            clicked {
                                bean.sn.notEmpty {
                                    goToOrderDetail("orderSn" to bean.sn)
                                }
                            }
                        }
                    }
                    .attachTo(layout)
            }
        }
    }

    override fun getData(index: Int, isLoading: Boolean) {
        getViewModel()
            .reportSum(
                page = index,
                type = mCouponRecordId,
                shopId = intent.getExtra("shopId")
            )
            .bindLifeCycle(getLifecycleOwner())
            .subscribeByFinally(
                event = getViewModel(),
                onSuccess = {
                    mList.apply {
                        (index == 0)
                            .condition {
                                clear()
                                pageNum = 0
                            }

                        addItems(it)
                        it.notEmpty { pageNum++ }
                    }

                    mList.onEach {
                        it as ReportCouponData
                        it.letter = TimeHelper
                            .getInstance()
                            .getMonthOfDate(
                                it.create_date.getTimeString(),
                                "yyyy-MM-dd HH:mm:ss"
                            )
                    }

                    mAdapter.setDataList(mList)
                },
                onFinally = {
                    isLoadingMore = false

                    mBinding
                        .listLayout
                        .apply {
                            swipeRefresh.isRefreshing = false

                            emptyLayout
                                .emptyView
                                .isVisible = mList.isEmpty()
                        }
                }
            )
    }

}